package main

import (
	"github.com/gin-gonic/gin"
	"github.com/yourusername/book-management-system/internal/config"
	"github.com/yourusername/book-management-system/internal/controller"
	"github.com/yourusername/book-management-system/internal/infrastructure/database"
	"github.com/yourusername/book-management-system/internal/infrastructure/persistence"
	"github.com/yourusername/book-management-system/internal/service"
)

func main() {
	// 加载配置
	cfg := config.LoadConfig()

	// 初始化数据库
	db, err := database.NewDB(cfg.DB)
	if err != nil {
		panic("failed to connect database")
	}

	// 初始化仓储
	bookRepo := persistence.NewBookRepository(db)
	borrowRepo := persistence.NewBorrowRepository(db)

	// 初始化服务
	bookService := service.NewBookService(bookRepo, borrowRepo)

	// 初始化控制器
	bookController := controller.NewBookController(bookService)

	// 设置路由
	r := gin.Default()

	api := r.Group("/api/v1")
	{
		books := api.Group("/books")
		{
			books.POST("", bookController.CreateBook)
			books.GET("", bookController.ListBooks)
			books.GET("/search", bookController.SearchBooks)
			books.GET("/:id", bookController.GetBook)
			books.PUT("/:id", bookController.UpdateBook)
			books.DELETE("/:id", bookController.DeleteBook)
			books.POST("/:id/borrow", bookController.BorrowBook)
			books.POST("/:id/return", bookController.ReturnBook)
		}
	}

	// 启动服务器
	r.Run(":" + cfg.Server.Port)
}
